/*
' Copyright (c) 2012-2013 Kyriakos Lesgidis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using net.diadrasis.CameraSlideshow.Components;

namespace net.diadrasis.CameraSlideshow.Data
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// SQL Server implementation of the abstract DataProvider class
    /// 
    /// This concreted data provider class provides the implementation of the abstract methods 
    /// from data dataprovider.cs
    /// 
    /// In most cases you will only modify the Public methods region below.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public class SqlDataProvider : DataProvider
    {

        #region Private Members

        private const string ProviderType = "data";
        private const string ModuleQualifier = "CameraSlideshow_";

        private readonly ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private readonly string _connectionString;
        private readonly string _providerPath;
        private readonly string _objectQualifier;
        private readonly string _databaseOwner;

        #endregion

        #region Constructors

        public SqlDataProvider()
        {

            // Read the configuration specific information for this provider
            Provider objProvider = (Provider)(_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);

            // Read the attributes for this provider

            //Get Connection string from web.config
            _connectionString = Config.GetConnectionString();

            if (string.IsNullOrEmpty(_connectionString))
            {
                // Use connection string specified in provider
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (!string.IsNullOrEmpty(_objectQualifier) && _objectQualifier.EndsWith("_", StringComparison.Ordinal) == false)
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (!string.IsNullOrEmpty(_databaseOwner) && _databaseOwner.EndsWith(".", StringComparison.Ordinal) == false)
            {
                _databaseOwner += ".";
            }

        }

        #endregion

        #region Properties

        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }

        public string ProviderPath
        {
            get
            {
                return _providerPath;
            }
        }

        public string ObjectQualifier
        {
            get
            {
                return _objectQualifier;
            }
        }

        public string DatabaseOwner
        {
            get
            {
                return _databaseOwner;
            }
        }

        private string NamePrefix
        {
            get { return DatabaseOwner + ObjectQualifier + ModuleQualifier; }
        }

        #endregion

        #region Private Methods

        private static object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        #region Public Methods

        #region Object

        public override IDataReader getObject(int moduleId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "tblObjectSelect",                
                new SqlParameter("@moduleId", moduleId)
            );
        }

        public override int addObject(cObject o)
        {
            return (Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, NamePrefix + "tblObjectInsert",
                 new SqlParameter("@objectAlignment", o.objectAlignment) 
                ,new SqlParameter("@objectAutoAdvance", o.objectAutoAdvance) 
                ,new SqlParameter("@objectTransitions", o.objectTransitions)
                ,new SqlParameter("@objectHeight", o.objectHeight)
                ,new SqlParameter("@objectHover", o.objectHover)
                ,new SqlParameter("@objectLoader", o.objectLoader)
                ,new SqlParameter("@objectLoaderColor", o.objectLoaderColor) 
                ,new SqlParameter("@objectLoaderBgColor", o.objectLoaderBgColor)
                ,new SqlParameter("@objectLoaderOpacity", o.objectLoaderOpacity)
                ,new SqlParameter("@objectNavigation", o.objectNavigation)
                ,new SqlParameter("@objectNavigationHover", o.objectNavigationHover) 
                ,new SqlParameter("@objectPlayPause", o.objectPlayPause)
                ,new SqlParameter("@objectPauseOnClick", o.objectPauseOnClick)
                ,new SqlParameter("@objectPieDiameter", o.objectPieDiameter)
                ,new SqlParameter("@objectPiePosition", o.objectPiePosition)
                ,new SqlParameter("@objectPortrait", o.objectPortrait)
                ,new SqlParameter("@objectThumbnails", o.objectThumbnails)
                ,new SqlParameter("@objectTime", o.objectTime)
                ,new SqlParameter("@objectTransPeriod", o.objectTransPeriod)
                ,new SqlParameter("@objectPagination", o.objectPagination)
                ,new SqlParameter("@moduleId", o.moduleId)
            )));
        }

        public override void updateObject(cObject o, int moduleId)
        {
            SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, NamePrefix + "tblObjectUpdate",
                new SqlParameter("@objectAlignment", o.objectAlignment) 
                ,new SqlParameter("@objectAutoAdvance", o.objectAutoAdvance) 
                ,new SqlParameter("@objectTransitions", o.objectTransitions)
                ,new SqlParameter("@objectHeight", o.objectHeight)
                ,new SqlParameter("@objectHover", o.objectHover)
                ,new SqlParameter("@objectLoader", o.objectLoader)
                ,new SqlParameter("@objectLoaderColor", o.objectLoaderColor) 
                ,new SqlParameter("@objectLoaderBgColor", o.objectLoaderBgColor)
                ,new SqlParameter("@objectLoaderOpacity", o.objectLoaderOpacity)
                ,new SqlParameter("@objectNavigation", o.objectNavigation)
                ,new SqlParameter("@objectNavigationHover", o.objectNavigationHover) 
                ,new SqlParameter("@objectPlayPause", o.objectPlayPause)
                ,new SqlParameter("@objectPauseOnClick", o.objectPauseOnClick)
                ,new SqlParameter("@objectPieDiameter", o.objectPieDiameter)
                ,new SqlParameter("@objectPiePosition", o.objectPiePosition)
                ,new SqlParameter("@objectPortrait", o.objectPortrait)
                ,new SqlParameter("@objectThumbnails", o.objectThumbnails)
                ,new SqlParameter("@objectTime", o.objectTime)
                ,new SqlParameter("@objectTransPeriod", o.objectTransPeriod)
                ,new SqlParameter("@objectPagination", o.objectPagination)
                ,new SqlParameter("@mId", moduleId));
        }

        public override void deleteObject(int moduleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "tblObjectDelete",                
                new SqlParameter("@mId", moduleId));
        }

        #endregion

        #region ObjectMedia

        public override IDataReader getMedia(int mediaObjectId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "tblMediaSelect",
                new SqlParameter("@mediaObjectId", mediaObjectId));
        }

        public override IDataReader getMediaByObject(int moduleId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "tblMediaSelectByObject",
                   new SqlParameter("@mId", moduleId));
        }

        public override int addMedia(cMediaObject m)
        {
            return (Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, NamePrefix + "tblMediaInsert",
                new SqlParameter("@mId", m.moduleId),
                new SqlParameter("@mediaObjectCaption", m.mediaObjectCaption),
                new SqlParameter("@mediaObjectTarget", m.mediaObjectTarget),
                new SqlParameter("@mediaObjectUrl", m.mediaObjectUrl))));
        }

        public override void updateMedia(cMediaObject m)
        {
            SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, NamePrefix + "tblMediaUpdate",
               new SqlParameter("@mediaObjectId", m.mediaObjectId),
               new SqlParameter("@mediaObjectCaption", m.mediaObjectCaption),
               new SqlParameter("@mediaObjectTarget", m.mediaObjectTarget),
               new SqlParameter("@mediaObjectUrl", m.mediaObjectUrl),
               new SqlParameter("@mId", m.moduleId));
        }

        public override void deleteMedia(int mediaObjectId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "tblMediaDelete",
                new SqlParameter("@mediaObjectId", mediaObjectId));
        }

        public override void deleteAllMedia(int moduleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "tblMediaDeleteAll",
                new SqlParameter("@mediaObjectId", moduleId));
        }

        #endregion

        #endregion

    }

}